<?php
$conn = mysql_connect('localhost', 'root', 'Gl-1114');
mysql_select_db('ddb');

header('Content-Type: application/json');
$start = $_REQUEST['start'];
$end   = $_REQUEST['end'];
$agentt = $_REQUEST['agent'];
$queuet = $_REQUEST['queuet'];
if($_REQUEST['act'] == 'total_sec'){
    $total_sec = mysql_fetch_array(mysql_query("SELECT
        SEC_TO_TIME(SUM(ABS((
        SELECT(unix_timestamp(`out_call`.`calldate`) + (`out_call`.`wait_time` + `out_call`.`duration`)) AS `end_date1`
        FROM  `out_call`
        WHERE  out_call.ext = outgoing.ext AND out_call.calldate > outgoing.calldate AND DATE(out_call.calldate) >= '$start' AND DATE(out_call.calldate) <= '$end' AND out_call.ext = '$agentt' AND TIME(out_call.calldate) >= '09:00:00' AND TIME(out_call.calldate) <= '22:00:00'
        LIMIT 1
    ) - (unix_timestamp(`outgoing`.`calldate`) + (`outgoing`.`wait_time` + `outgoing`.`duration`))))) AS `dro`
        FROM `out_call` AS outgoing
        WHERE DATE(outgoing.calldate) >= '$start' AND DATE(outgoing.calldate) <= '$end' AND outgoing.ext = '$agentt' AND TIME(outgoing.calldate) >= '09:00:00' AND TIME(outgoing.calldate) <= '22:00:00'
        ORDER BY outgoing.calldate"));
    
    $total_talk_sec = mysql_fetch_array(mysql_query("SELECT SEC_TO_TIME(SUM(out_call.duration))
                                                     FROM out_call
                                                     WHERE DATE(out_call.calldate) >= '$start'
                                                     AND DATE(out_call.calldate) <= '$end'
                                                     AND out_call.ext = '$agentt'
                                                     AND TIME(out_call.calldate) >= '09:00:00' AND TIME(out_call.calldate) <= '22:00:00'"));

        $sec = array("sec"	=> array(),"talk" => array());
        $sec['sec'][] = $total_sec[0];
        $sec['talk'][] = $total_talk_sec[0];
        echo json_encode($sec);
}else{
$quantity = array();
$cause = array();
$cause1 = array();

$name = array();
$agent = array();

$ress =mysql_query("
                    SELECT                   	
                    	   `outgoing`.`calldate` AS `calldate`, 
                            (SELECT FROM_UNIXTIME(unix_timestamp(`out_call`.`calldate`) + (`out_call`.`wait_time` + `out_call`.`duration`)) AS `end_date1`
                        	FROM  `out_call`
                        	WHERE  out_call.ext = outgoing.ext AND out_call.calldate > outgoing.calldate AND DATE(out_call.calldate) >= '$start' AND DATE(out_call.calldate) <= '$end' AND out_call.ext = '$agentt' AND TIME(out_call.calldate) >= '09:00:00' AND TIME(out_call.calldate) <= '22:00:00'
                        	LIMIT 1) AS `end`,         	
                        	ABS((
                        	SELECT(unix_timestamp(`out_call`.`calldate`) + (`out_call`.`wait_time` + `out_call`.`duration`)) AS `end_date1`
                        	FROM  `out_call`
                        	WHERE  out_call.ext = outgoing.ext AND out_call.calldate > outgoing.calldate AND DATE(out_call.calldate) >= '$start' AND DATE(out_call.calldate) <= '$end' AND out_call.ext = '$agentt' AND TIME(out_call.calldate) >= '09:00:00' AND TIME(out_call.calldate) <= '22:00:00'
                        	LIMIT 1
                        	) - (unix_timestamp(`outgoing`.`calldate`) + (`outgoing`.`wait_time` + `outgoing`.`duration`))) AS `dro`
                    FROM `out_call` AS outgoing
                    WHERE DATE(outgoing.calldate) >= '$start' AND DATE(outgoing.calldate) <= '$end' AND outgoing.ext = '$agentt' AND TIME(outgoing.calldate) >= '09:00:00' AND TIME(outgoing.calldate) <= '22:00:00'
                    ORDER BY outgoing.calldate
                    ");
    	
    while($row1 = mysql_fetch_assoc($ress)){

    $call_count[]   = (float)$row1[dro];
    $agent[]		= $row1[calldate].' - '. $row1[end];
}

$unit[]=" წამი";
$series[] = array('name' => $name, 'unit' => $unit, 'call_count' => $call_count, 'agent' => $agent);

echo json_encode($series);

}

?>